package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.StaffMapper;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.Staff;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IStaffService;

/**
 * 员工信息Service接口实现类（OSI_STAFF）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("staffService")
public class StaffServiceImpl implements IStaffService {

	@Resource
	private StaffMapper staffMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.staffMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.staffMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(Staff record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.staffMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Staff record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.staffMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(Staff record) throws Exception {
		record.setUpdateTime(new Date());
		return this.staffMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Staff record) throws Exception {
		record.setUpdateTime(new Date());
		return this.staffMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Staff selectByPrimaryKey(String id) throws Exception {
		return this.staffMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Staff> selectList(Staff record) throws Exception {
		return this.staffMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Staff> selectListByPage(Map<String, Object> params, BasePage<Staff> page) throws Exception {
		params.put("page", page);
		List<Staff> list = this.staffMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Staff record, UserInfo userInfo) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());

		if (StringUtils.isBlank(userInfo.getId())) {
			userInfo.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		userInfo.setCreateTime(new Date());

		record.setAccount(userInfo.getAccount());
		record.setUserId(userInfo.getId());
		this.userInfoMapper.insertSelective(userInfo);
		return this.staffMapper.insertSelective(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Staff record, UserInfo userInfo) throws Exception {
		record.setUpdateTime(new Date());
		userInfo.setUpdateTime(new Date());
		this.userInfoMapper.updateByPrimaryKeySelective(userInfo);
		return this.staffMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询员工编号
	 * 
	 * @return
	 * @throws Exception
	 */
	public String selectStaffNum() throws Exception {
		return this.staffMapper.selectStaffNum();
	}

	/**
	 * 批量更改部门状态
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStatusByIds(Map<String, Object> params) throws Exception {
		return this.staffMapper.updateStatusByIds(params);
	}
}